<!--楼宇设备-->
<template>
  <basic-layout title="办公物资修改" :showAddBtn="false">
    <van-form @submit="saveHandle" ref="form" class="dataStyle">
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.SWID"
        label="实物ID"
        placeholder=""
        :border="false"
      />
      <van-field
        required
        v-model="dataInfo.PINM"
        label="设备名称"
        placeholder="请输入设备名称"
        :border="false"
        :rules="rules.PINM"
      />
      <van-field
        required
        v-model="dataInfo.XINGH"
        label="设备型号"
        placeholder="请输入设备型号"
        :border="false"
        :rules="rules.XINGH"
      />
      <van-field
        readonly
        required
        @click="showDialogHandle(99)"
        v-model="dataInfo.UNIT"
        label="单位"
        placeholder="请选择单位"
        :border="false"
        is-link
        :rules="rules.UNIT"
      />
      <van-field
        v-model="dataInfo.DANJ"
        label="单价(元)"
        placeholder="请输入单价(元)"
        :border="false"
        :rules="rules.DANJ"
      />
      <van-field
        readonly
        required
        @click="showDialogHandle(1)"
        v-model="dataInfo.WZDL"
        label="物资大类"
        placeholder="请选择物资大类"
        :border="false"
        is-link
        :rules="rules.WZDL"
      />
      <van-field
        @click="showDialogHandle(2)"
        readonly
        required
        v-model="dataInfo.WZXL"
        label="物资小类"
        placeholder="请选择物资小类"
        is-link
        :rules="rules.WZXL"
      />
      <van-field
        :readonly="true"
        :disabled="true"
        label="楼宇信息"
        placeholder=""
        v-model="dataInfo.FANGCNAME"
      />
      <van-field
        v-model="dataInfo.LOUCNAME"
        :readonly="true"
        :disabled="true"
        label="楼层信息"
        placeholder=""

      />
      <van-field
        v-model="dataInfo.FANGJNAME"
        :readonly="true"
        :disabled="true"
        label="房间信息"
        placeholder=""
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.GYSMC"
        label="供应商"
        placeholder=""
      />
      <van-field
        required
        v-model="dataInfo.SHENGCCJ"
        label="生产厂家"
        placeholder="请输入生产厂家"
        :rules="rules.SCCJ"
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.SHENGCSJ"
        label="生产日期"
        placeholder=""
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.RUKSJ"
        label="入库日期"
        placeholder=""
      />
      <van-field
        required
        v-model="dataInfo.SHIYQX"
        label="使用年限(月)"
        placeholder="请输入使用年限"
        :rules="rules.SHIYQX"
      />
      <van-field
        @click="showDialogHandle(6)"
        readonly
        required
        v-model="dataInfo.BAOGRNAME"
        label="保管人"
        placeholder="请选择保管人"
        is-link
        :rules="rules.BAOGRNAME"
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.ERPTAG"
        label="ERP编码"
        placeholder=""
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.BATCH1"
        label="采购批次"
        placeholder="请输入采购批次"
      />
      <van-field
        :readonly="true"
        :disabled="true"
        v-model="dataInfo.STATUS"
        label="状态"
        placeholder=""
      />
      <div class="remarkDownTextStyle">
        <div>&nbsp;&nbsp;&nbsp;&nbsp;备注</div>
        <van-field
          v-model="dataInfo.REMARK"
          rows="2"
          autosize
          label=""
          type="textarea"
          placeholder="请输入备注"
        />
      </div>
    </van-form>
    <van-action-sheet
      v-model="showUnitPicker"
      cancel-text="取消"
      :actions="data.defaultData.dictDatas"
      @select="UnitonConfirm"
    />
    <van-action-sheet
      v-model="showBaoPicker"
      cancel-text="取消"
      :actions="data.defaultData.BaogrDatas"
      @select="choosePeopleHandle"
    />
    <van-action-sheet
      v-model="showPicker"
      cancel-text="取消"
      :actions="data.defaultData.dlDataList"
      @select="onConfirm"
    />
    <van-action-sheet
      v-model="smallPicker"
      cancel-text="取消"
      :actions="XLItem"
      @select="SubclassOnConfirm"
    />
    <van-action-sheet
      v-model="showLyPicker"
      cancel-text="取消"
      :actions="data.defaultData.lyDataList"
      @select="buildingOnConfirm"
    />
    <van-action-sheet
      v-model="showLcPicker"
      cancel-text="取消"
      :actions="FcItem"
      @select="builLcOnConfirm"
    />
    <van-action-sheet
      v-model="showFjPicker"
      cancel-text="取消"
      :actions="FjItem"
      @select="builFjOnConfirm"
    />
    <!--        <van-button type="primary" size="large" color="#4eb3e9" @click="saveHandle">保存</van-button>-->

    <div class="btnBox">
      <van-button
        type="primary"
        color="#4eb3e9"
        size="large"
        @click="saveHandle"
        >保存</van-button
      >
    </div>
  </basic-layout>
</template>
<script>
import {
  getLbByType,
  getLouyByUser,
  getBangwzQuanxst,
  getLoucByLouy,
  getFangjByLouc,
  saveBangwz,
} from "@/api/srvAssurance/assetLabel/assetLabel";
import peopleChoose from "./serviceEquipmentPeopleChoose";
import { queryDic } from "@/api/common";
import { queryWin } from "@/api/srvAssurance/assetLabel/assetLabel";

export default {
  name: "officeMaterialRegisterEdit",
  components: { peopleChoose },

  // setup () {
  //   const searchVal = ref('')
  //   return { searchVal }
  // },

  data() {
    return {
      loading: false,
      formData: {
        id: "",
        louyxx: "",
        loucyy: "",
        fangjxx: "",
        baogr: "",
        wzdl: "",
        wzxl: "",
        swid: "",
        gongys: "",
        shengccj: "",
        pinm: "",
        xingh: "",
        unit: "",
        danj: "",
        ruksj: "",
        shengcsj: "",
        peizsj: "",
        tuiysj: "",
        shiyqx: "",
        BATCH1: "",
        status: "",
        shiftq: "",
        remark: "",
      },
      rules: {
        PINM: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        XINGH: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        UNIT: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        DANJ: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        WZDL: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
        WZXL: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
        FANGCNAME: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
        LOUCNAME: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
        FANGJNAME: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
        SHENGCCJ: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        SHIYQX: [
          {
            required: true,
            message: "",
            trigger: "onBlur",
          },
        ],
        BAOGRNAME: [
          {
            required: true,
            message: "",
            trigger: "onChange",
          },
        ],
      },

      dataInfo: {},
      showDialog: false,
      showUnitPicker: false,
      showBaoPicker: false,
      showPicker: false,
      smallPicker: false,
      showLyPicker: false,
      showLcPicker: false,
      showFjPicker: false,
      dialogTitle: "",
      searchVal: "",
      dialogData: [],
      dialogDataBack: [],
      UnitItem: [],
      XLItem: [],
      BaogrItem: [],
      FcItem: [],
      FjItem: [],
      data: {
        defaultData: {
          dlDataList: [], //大类数据
          xlDataList: [], //小类数据
          lyDataList: [], //楼宇数据
          lcDataList: [], //楼层数据
          fjDataList: [], //房间数据
          dictDatas: [], //单位字典数据
          BaogrDatas: [], //保管人
        },
        filterData: {
          dlDataList: [], //大类数据
          xlDataList: [], //小类数据
          lyDataList: [], //楼宇数据
          lcDataList: [], //楼层数据
          fjDataList: [], //房间数据
          dictDatas: [], //单位字典数据
          BaogrDatas: [], //保管人
        },
      },
      flag: null,
    };
  },

  mounted() {
    this.getDicts();
  },

  created() {
    if (this.$router.history.current.query.id) {
      this.initData(this.$router.history.current.query.id);
    }
    this.initLbType();
    this.initLyData();
  },

  methods: {
    // 获取所有字典
    getDicts: function () {
      // 获取单位字段
      queryDic("RA_WORK_UNIT_CODE").then((res) => {
        this.data.defaultData.dictDatas = [];
        if (res.code === 1000 && res.data) {
          res.data.forEach((item) => {
            var data = {};
            data.name = item.name;
            data.dwId = item.value;
            this.data.defaultData.dictDatas.push(data);
          });
        }
      });
    },

    choosePeopleHandle(val) {
      console.log(val);
      this.dataInfo.BAOGRNAME = val.NAME;
      this.dataInfo.BAOGR =  val.value
      this.showBaoPicker = false;
    },

    async initLbType() {
      let lbDatas = await getLbByType({ type: "1" });
      if (lbDatas.status == 0) {
        var dataList = lbDatas.data;
        dataList.forEach((item) => {
          if (item.parentCode == "") {
            this.data.defaultData.dlDataList.push(item);
          } else {
            this.data.defaultData.xlDataList.push(item);
          }
        });
      }
    },

    async initLyData() {
      let queryParam = {};
      queryParam.corpid =
        sessionStorage.getItem("corpId") || "40286c1484bdd1a10184e137ceea005f";
      let louYuDatas = await getLouyByUser(queryParam);
      if (louYuDatas.status == 0) {
        var dataList = louYuDatas.data;
        dataList.forEach((item) => {
          item.name = item.fangcname;
          this.data.defaultData.lyDataList.push(item);
        });
      }
    },

    async initData(id) {
      let resData = await getBangwzQuanxst({ id: id });
      this.dataInfo = {};
      if (resData.status === 0) {
        this.dataInfo = resData.data;
      }
    },

    saveHandle() {
      this.$refs.form
        .validate()
        .then(async () => {
          this.dealData();
          this.loading = true;
          var res = await saveBangwz(this.formData);
          this.loading = false;
          if (res.status === 0) {
            this.$toast.success("修改成功");
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    dealData() {
      this.formData.id = this.dataInfo.ID;
      this.formData.louyxx = this.dataInfo.LOUYXX;
      this.formData.loucxx = this.dataInfo.LOUCXX;
      this.formData.fangjxx = this.dataInfo.FANGJXX;
      this.formData.baogr = this.dataInfo.BAOGR;
      this.formData.wzdl = this.dataInfo.WZDLID;
      this.formData.wzxl = this.dataInfo.WZXLID;
      this.formData.swid = this.dataInfo.SWID;
      this.formData.gongys = this.dataInfo.GONGYS;
      this.formData.shengccj = this.dataInfo.SHENGCCJ;
      this.formData.pinm = this.dataInfo.PINM;
      this.formData.xingh = this.dataInfo.XINGH;
      this.formData.unit = this.dataInfo.UNITID;
      this.formData.danj = this.dataInfo.DANJ;
      this.formData.ruksj = this.dataInfo.RUKSJ;
      this.formData.shengcsj = this.dataInfo.SHENGCSJ;
      this.formData.peizsj = this.dataInfo.PEIZSJ;
      this.formData.tuiysj = this.dataInfo.TUIYSJ;
      this.formData.shiyqx = this.dataInfo.SHIYQX;
      this.formData.status = this.dataInfo.STATUSID;
      // this.formData.shiftq = this.dataInfo.SHIFTQ;
      this.formData.remark = this.dataInfo.REMARK;
    },

    onConfirm(val) {
      this.dataInfo.WZDL = val.name;
      this.dataInfo.WZDLID = val.code;
      this.canteenCode = val.code;
      this.showPicker = false;
    },
    SubclassOnConfirm(val) {
      this.dataInfo.WZXL = val.name;
      this.dataInfo.WZXLID = val.code;
      this.smallPicker = false;
      this.XLItem = [];
    },
    buildingOnConfirm(val) {
      this.dataInfo.FANGCNAME = val.name;
      this.dataInfo.LOUYXX = val.fangcid;
      this.showLyPicker = false;
    },
    builLcOnConfirm(val) {
      console.log(val.loucid);
      this.dataInfo.LOUCNAME = val.name;
      this.dataInfo.LOUCXX = val.loucid;
      this.showLcPicker = false;
      this.FcItem = [];
    },
    builFjOnConfirm(val) {
      this.dataInfo.FANGJNAME = val.name;
      this.dataInfo.FANGJXX = val.fangjid;
      this.showFjPicker = false;
      this.FjItem = [];
    },
    UnitonConfirm(val) {
      this.dataInfo.UNIT = val.name;
      this.dataInfo.UNITID = val.fangjid;
      this.showUnitPicker = false;
    },
    async showDialogHandle(val) {
      this.dialogDataBack = [];
      if (val === 1) {
        this.showPicker = true;
      } else if (val === 2) {
        if (!this.dataInfo.WZDLID) {
          this.$toast("请先选择大类");
          return;
        }
        this.XLItem = [];
        // 处理小类数据
        let code = this.dataInfo.WZDLID;
        this.data.defaultData.xlDataList.forEach((item) => {
          if (item.parentCode === code) {
            this.XLItem.push(item);
          }
        });
        this.smallPicker = true;
      } else if (val === 3) {
        this.showLyPicker = true;
      } else if (val === 4) {
        if (!this.dataInfo.LOUYXX) {
          this.$toast("请先选择楼宇信息");
          return;
        }
        let lcData = await getLoucByLouy({ louyid: this.dataInfo.LOUYXX });
        this.FcItem = [];
        if (lcData.status == 0) {
          lcData.data.forEach((item) => {
            var data = {};
            data.loucid = item.loucid;
            data.name = item.loucname;
            this.FcItem.push(data);
          });
        }
        this.showLcPicker = true;
      } else if (val === 5) {
        if (!this.dataInfo.LOUYXX) {
          this.$toast("请先选择楼宇信息");
          return;
        } else if (!this.dataInfo.LOUCXX) {
          this.$toast("请先选择楼层信息");
          return;
        } else {
          let fjData = await getFangjByLouc({ loucid: this.dataInfo.LOUCXX });
          this.FcItem = [];
          if (fjData.status == 0) {
            fjData.data.forEach((item) => {
              var data = {};
              data.fangjid = item.fangjid;
              data.name = item.fangjname;
              this.FjItem.push(data);
            });
          }
          this.showFjPicker = true;
        }
      } else if (val == 99) {
        this.showUnitPicker = true;
      } else if (val == 6) {
        this.data.defaultData.BaogrDatas = [];
        let queryParam = {
          corpid: "",
          pageIndex: 1,
          pageSize: 20,
        };
        queryParam.corpid =
          sessionStorage.getItem("corpId") ||
          "40286c1484bdd1a10184e137ceea005f";
        let BaogrDatas = await queryWin(queryParam);
        var dataList = BaogrDatas.data;
        dataList.forEach((item) => {
          item.name = item.NAME;
          item.value = item.staff_id;
          this.data.defaultData.BaogrDatas.push(item);
        });
        this.showBaoPicker = true;
      }
      this.showDialog = true;
      this.flag = val;
    },

    chooseHandle(item) {
      this.showDialog = false;
      if (this.flag === 1) {
        this.dataInfo.WZDL = item.name;
        this.dataInfo.WZDLID = item.code;

        this.dataInfo.WZXL = null;
        this.dataInfo.WZXLID = null;
      } else if (this.flag === 2) {
        this.dataInfo.WZXL = item.name;
        this.dataInfo.WZXLID = item.code;
      } else if (this.flag === 3) {
        this.dataInfo.FANGCNAME = item.name;
        this.dataInfo.LOUYXX = item.fangcid;

        this.dataInfo.LOUCNAME = null;
        this.dataInfo.LOUCXX = null;

        this.dataInfo.FANGJNAME = null;
        this.dataInfo.FANGJXX = null;
      } else if (this.flag === 4) {
        this.dataInfo.LOUCNAME = item.name;
        this.dataInfo.LOUCXX = item.loucid;

        this.dataInfo.FANGJNAME = null;
        this.dataInfo.FANGJXX = null;
      } else if (this.flag === 5) {
        this.dataInfo.FANGJNAME = item.name;
        this.dataInfo.FANGJXX = item.fangjid;
      } else if (this.flag === 99) {
        this.dataInfo.UNIT = item.name;
        this.dataInfo.UNITID = item.dwId;
      }
    },

    clearHandle() {
      this.showDialog = false;
      if (this.flag === 1) {
        this.dataInfo.WZDL = null;
        this.dataInfo.WZDLID = null;

        this.dataInfo.WZXL = null;
        this.dataInfo.WZXLID = null;
      } else if (this.flag === 2) {
        this.dataInfo.WZXL = null;
        this.dataInfo.WZXLID = null;
      } else if (this.flag === 3) {
        this.dataInfo.FANGCNAME = null;
        this.dataInfo.LOUYXX = null;

        this.dataInfo.LOUCNAME = null;
        this.dataInfo.LOUCXX = null;

        this.dataInfo.FANGJNAME = null;
        this.dataInfo.FANGJXX = null;
      } else if (this.flag === 4) {
        this.dataInfo.LOUCNAME = null;
        this.dataInfo.LOUCXX = null;

        this.dataInfo.FANGJNAME = null;
        this.dataInfo.FANGJXX = null;
      } else if (this.flag === 5) {
        this.dataInfo.FANGJNAME = null;
        this.dataInfo.FANGJXX = null;
      } else if (this.flag === 99) {
        this.dataInfo.UNIT = null;
        this.dataInfo.UNITID = null;
      }
    },

    onUpdate(value) {
      value = value.trim();
      this.dialogDataBack = [];
      let dialogData = [];
      if (this.flag === 1) {
        dialogData = this.data.defaultData.dlDataList;
      } else if (this.flag === 2) {
        dialogData = this.data.defaultData.xlDataList;
      } else if (this.flag === 3) {
        dialogData = this.data.defaultData.lyDataList;
      } else if (this.flag === 4) {
        dialogData = this.data.defaultData.lcDataList;
      } else if (this.flag === 5) {
        dialogData = this.data.defaultData.fjDataList;
      }
      if (value == null || value.trim() == "") {
        this.dialogDataBack = [...dialogData];
        return;
      }
      if (value) {
        dialogData.forEach((i) => {
          let val = { ...i };
          //判断遍历是否包含输入字符
          if (val.name.indexOf(value) != -1) {
            var newHtml = `<span style="color: red">${value}</span>`;
            //实现高亮效果
            this.dialogDataBack.push(val);
            //替换高亮字段
            this.dialogDataBack.forEach((item) => {
              item.value = item.name;
              item.value = item.value.replace(value, newHtml);
            });
          }
        });
      }
    },
  },
};
</script>

<style scoped lang="less">
.dataStyle {
  height: calc(100% - 38px);
  overflow: auto;
}

.van-dialog {
  height: 70%;
}

.van-dialog__content {
  overflow-y: scroll;
  height: 90%;
}

.dialogDataStyle {
  text-align: center;
}
.parent-element {
  display: flex;
  justify-content: center;
}

.btnBox {
  width: 100%;
  position: fixed;
  bottom: 0px;
  height: 40px;
  margin: 20px auto;
  // padding: 0 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.btnBox /deep/ .van-button--large {
}

.van-button--large {
  height: 40px;
  line-height: 40px;
  width: 95%;
  margin: 10px auto;
}
</style>
